/*
  学习目标：掌握修改store中的数据
*/

import { legacy_createStore as createStore } from 'redux';

const store = createStore(function (
  state = { count: 0, msg: 'hello 83' },
  action
) {
  // 2. reducer函数，接收state和action， 返回一个新的state
  if (action.type === 'add') {
    //💥 不可变数据
    return {
      ...state,
      count: state.count + 1,
    };
  }

  if (action.type === 'des') {
    return {
      ...state,
      count: state.count - 1,
    };
  }
  return state;
});

console.log('store.getState().count  ----->  ', store.getState().count);
// 1. dispatch发起一个action
// 注意：💥dispatch必须接收一个action参数
// 注意：💥action是一个普通的JS对象， 必须有type属性
store.dispatch({ type: 'add' });

console.log('store.getState().count  ----->  ', store.getState().count);

store.dispatch({ type: 'des' });

console.log('store.getState().count  ----->  ', store.getState().count);
